// pages/userCenter/creatMyShop/index.js
// pages/divide/index.js
//var COS = require('../../lib/cos-wx-sdk-v5.js')
var COS = require('../../../lib/cos-wx-sdk-v5.js')
var cos = new COS({
  SecretId: 'AKIDq9G6rMid00sToTTpXy2rSln7IFvSoMkq',
    SecretKey: 'FMQDwQhD7mOmCOM6D4buw1qhKfmX3KYW',
});
const app = getApp()
const ip=app.globalData.ip
// 接下来可以通过 cos 实例调用 COS 请求。
// TODO

Page({

  /**
   * 页面的初始数据
   */
  data: {
    //图片
    img:'',
    //展示图片
    show:false,
    //展示上传按钮
    num:true,
    videonum:true,
    videoshow:false,
    videonum2:false,
    videoshow2:true,
    //展示图片得删除图标
    //icon:false
    //首图
    ShopBanner:'',
    BannericonShow:true,
    BannerShow:false,
    //名称、范围、简介
    shopName:'',
    describe:'',
    scop:'',
    //视频封面
    viedoImg:'',
    videoUrl:'',
    videoUrll:'',
//
storeid:0,
nowstore:{}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    if(options.storeid!=null){
      this.setData({
        storeid:options.storeid
      })
      wx.request({
        url: ip+'/store/SelectStoreDetail',
        data:{
          storeid:options.storeid
        },
        success:res=>{
          console.log(res)
          this.setData({
            nowstore:res.data.data,
            /*封面更新隐藏*/ 
            BannericonShow:false,
            BannerShow:true,
            ShopBanner:res.data.data.imgShop,
            /*案例图片*/
            show:true,
            num:false,
            img:res.data.data.imgCase,
            /*视频 */
            videoUrll:ip+res.data.data.imgAuth,
            videoshow:true,
            videonum:false,
            videonum2:true,
          })
        }
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function (res) {
    this.videoContext = wx.createVideoContext('myVideo')
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  //上传图片
  upload(){
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['original'], // 可以指定是原图还是压缩图，默认用原图
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: res=> {
          var filePath = res.tempFiles[0].path;
          var filename = filePath.substr(filePath.lastIndexOf('/') + 1);
          cos.postObject({
            // 找到存储桶
              Bucket: 'xiang-1303170601',
              //找到api地区
              Region: 'ap-shanghai',
              Key:filename,
              FilePath: filePath,
              onProgress: function (info) {
                  console.log(JSON.stringify(info));
              }
          }, (err, data) =>{
            //成功回调
              console.log(err || data);
              //console.log(data.headers.location)
              let img=data.headers.location
              this.setData({
                show:true,
                num:false,
                //icon:true,
                img:img
              })
          });
      }
    });
  },
  //删除
  delete(){
      let img='';
      this.setData({
        img:img,
        //icon:false,
        show:false,
        num:true,
      })
  },
  //小店封面
  ShopBanner(){
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['original'], // 可以指定是原图还是压缩图，默认用原图
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: res=> {
        console.log(res)
          var filePath = res.tempFiles[0].path;
          var filename = filePath.substr(filePath.lastIndexOf('/') + 1);
          cos.postObject({
            // 找到存储桶
              Bucket: 'xiang-1303170601',
              //找到api地区
              Region: 'ap-shanghai',
              Key:filename,
              FilePath: filePath,
              onProgress: function (info) {
                  console.log(JSON.stringify(info));
              }
          }, (err, data) =>{
            //成功回调
              console.log(err || data);
              //console.log(data.headers.location)
              let ShopBanner=data.headers.location
              this.setData({
                ShopBanner:ShopBanner,
                BannericonShow:false,
                BannerShow:true
              })
          });
      }
    });
  },
  //删除首图
  deleteBanner(){
    let nowstoreimgshop=this.nowstore
    if(nowstoreimgshop!=null){
      this.setData({
        nowstore:{}
      })
    }
    let ShopBanner='';
    this.setData({
      ShopBanner:ShopBanner,
      BannericonShow:true,
      BannerShow:false
    })
  },
  //店名输入
  userNameInput(e){
    console.log(e);
    let shopName=e.detail.value;
    this.setData({
      shopName:shopName
    })
  },
  //范围输入
  passWdInput(e){
    let scop=e.detail.value;
    this.setData({
      scop:scop
    })
  },
  //简介输入
  describeWdInput(e){
    let describe=e.detail.value;
    this.setData({
      describe:describe
    })
  },
  addStore(e){
    let userid=wx.getStorageSync('userid');
    let ShopBanner=this.data.ShopBanner;
    let img=this.data.img;
    let shopName=this.data.shopName;
    let scop=this.data.scop;
    let describe=this.data.describe;
    wx.request({
      url: ip+"/store/AddStore",
      data:{
        userId:userid,
        shopName:shopName,
        scope:scop,
        present:describe,
        imgShop:ShopBanner,
        imgCase:img,
        imgAuth:this.data.videoUrl
      },
      method:"POST",
      header: {
        "Content-Type": "application/json"
        },
        success:res=>{
          console.log(res)
          if(res.data.data==null){
            wx.showToast({
              title: res.data.msg,
              icon:'none'
            })
            return 0
          }
          wx.setStorage({
            key:"storeid",
            data:res.data.data,
          });
          wx.showToast({
            title: '操作成功！', // 标题
            icon: 'success',  // 图标类型，默认success
            duration: 40000,  // 提示窗停留时间，默认1500ms
            success(){
              wx.switchTab({
                url: '../../userCenter/index',
              })
            }
          })
        }
    })
  },
  //上传视频
  uploadVideo(){
    wx.chooseVideo({
      sourceType: ['album','camera'],
      maxDuration: 60,
      camera: 'back',
      success:(res) =>{
        console.log(res)
      
        wx.uploadFile({
          filePath: res.tempFilePath,
          name: 'file',
          url: ip+'/File/uploadFile',
          success:res=>{
            console.log(res)
            let jsonObject=JSON.parse(res.data);
            console.log(ip+jsonObject['data'])
            this.setData({
              videoUrll:ip+jsonObject['data'],
              videoUrl:jsonObject['data'],
              videonum:false,
              videoshow:true,
              videonum2:true,
            })
          }
        })
      }
    })
  },
  uploadVideo2(){
    wx.chooseVideo({
      sourceType: ['album','camera'],
      maxDuration: 60,
      camera: 'back',
      success:(res) =>{
        console.log(res)
      
        wx.uploadFile({
          filePath: res.tempFilePath,
          name: 'file',
          url: ip+'/File/uploadFile',
          success:res=>{
            console.log(res)
            let jsonObject=JSON.parse(res.data);
            console.log(ip+jsonObject['data'])
            this.setData({
              videoUrll:ip+jsonObject['data'],
              videoUrl:jsonObject['data'],
              // videonum2:false,
              // videoshow2:true,
            })
          }
        })
      }
    })
  },


  bindInputBlur: function(e) {
    this.inputValue = e.detail.value
  },
  bindSendDanmu: function () {
    this.videoContext.sendDanmu({
      text: this.inputValue,
      color: getRandomColor()
    })
  },
  bindPlay: function() {
    this.videoContext.play()
  },
  bindPause: function() {
    this.videoContext.pause()
  },
  videoErrorCallback: function(e) {
    console.log('视频错误信息:')
    console.log(e.detail.errMsg)
  },

  updateStore(){
    let userid=wx.getStorageSync('userid');
    let ShopBanner=this.data.ShopBanner;
    let img=this.data.img;
    let shopName=this.data.shopName;
    let scop=this.data.scop;
    let describe=this.data.describe;
    wx.request({
      url: ip+"/store/updateStore",
      data:{
        userId:userid,
        shopName:shopName,
        scope:scop,
        present:describe,
        imgShop:ShopBanner,
        imgCase:img,
        imgAuth:this.data.videoUrl
      },
      method:"POST",
      header: {
        "Content-Type": "application/json"
        },
        success:res=>{
          console.log(res)
          wx.setStorage({
            key:"storeid",
            data:res.data.data,
          });
          wx.showToast({
            title: '操作成功！', // 标题
            icon: 'success',  // 图标类型，默认success
            duration: 40000,  // 提示窗停留时间，默认1500ms
            success(){
              wx.switchTab({
                url: '../../userCenter/index',
              })
            }
          })
        }
    })
  }
})